class Solution
{
    vector<vector<int>> ret;
    vector<int> path;
    vector<bool> check;
public:
    vector<vector<int>> permuteUnique(vector<int>& nums)
    {
        sort(nums.begin(), nums.end());
        check.resize(nums.size());
        dfs(nums);
        return ret;
    }
    void dfs(vector<int>& nums)
    {
        if (path.size() == nums.size())
        {
            ret.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); i++)
        {
            if (check[i] == true || (i != 0 && nums[i - 1] == nums[i] && check[i - 1] == false)) continue;
            path.push_back(nums[i]);
            check[i] = true;
            dfs(nums);
            check[i] = false;
            path.pop_back();
        }
    }
};

class Solution
{
    long prev_min = LONG_MIN;
public:
    bool isValidBST(TreeNode* root)
    {
        return dfs(root);
    }
    bool dfs(TreeNode* root)
    {
        if (root == nullptr) return true;
        bool left = dfs(root->left);
        if (left == false) return false;
        if (prev_min >= root->val) return false;
        prev_min = root->val;
        bool right = dfs(root->right);
        if (right == false) return false;
        return true;
    }
};